package com.util.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-28 09:19:51
 */
public class RenderMain {

    private final BlockingQueue<Callable<?>> delayQueue; // 延时队列
    private final BlockingQueue<Callable<?>> interactionQueue; // 交互队列
    private final BlockingQueue<Callable<?>> microQueue; // 微队列

    public RenderMain(BlockingQueue<Callable<?>> delayQueue, BlockingQueue<Callable<?>> interactionQueue,
                      BlockingQueue<Callable<?>> microQueue) {
        this.delayQueue = delayQueue;
        this.interactionQueue = interactionQueue;
        this.microQueue = microQueue;
    }

    public Object render() {
        Callable<?> callable;
        if (microQueue.size() > 0) {
            callable = microQueue.poll();
        } else if (interactionQueue.size() > 0) {
            callable = interactionQueue.poll();
        } else if (delayQueue.size() > 0) {
            callable = delayQueue.poll();
        } else {
            return null;
        }

        try {
            return Executors.newFixedThreadPool(1).submit(callable).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

}
